package com.example.springboot.test;

import com.example.springboot.request.DocQueryRequest;
import com.example.springboot.request.DocSaveRequest;
import com.example.springboot.response.DocQueryResponse;
import com.example.springboot.response.CommonResponse;
import com.example.springboot.response.PageResponse;
import com.example.springboot.service.ServiceDoc;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;


@RestController//表示访问这个地址时要返回一个字符串
public class DocController {

    //查询all
    @Resource
    private ServiceDoc serviceDoc;
    @GetMapping("/Doc/all/{ebookId}")
    public CommonResponse all(@PathVariable Long ebookId){
        CommonResponse<List<DocQueryResponse>> response=new CommonResponse<>();
       List<DocQueryResponse> list= serviceDoc.all(ebookId);
        response.setContent(list);
        return response;
    }

    //查询2
    @GetMapping("/Doc/service")
    public CommonResponse list(@Valid DocQueryRequest docQueryRequest){
        CommonResponse<PageResponse<DocQueryResponse>> response=new CommonResponse<>();
        PageResponse<DocQueryResponse> list= serviceDoc.list(docQueryRequest);
        response.setContent(list);
        return response;
    }

    //保存
    @PostMapping("/Doc/save")
    public CommonResponse save(@Valid @RequestBody DocSaveRequest docQueryRequest){
        CommonResponse response=new CommonResponse<>();
        serviceDoc.save(docQueryRequest);
        return response;
    }

    //删除
    @DeleteMapping("/Doc/delete/{idsStr}")
    public CommonResponse delete(@PathVariable String idsStr){
        CommonResponse response=new CommonResponse<>();
        List<String> list = Arrays.asList(idsStr.split(","));
        serviceDoc.delete(list);
        return response;
    }

    //查询2
    @GetMapping("/Doc/find-content/{id}")
    public CommonResponse findContent(@PathVariable Long id){
        CommonResponse<String> response=new CommonResponse<>();
        String content= serviceDoc.findContent(id);
        response.setContent(content);
        return response;
    }

    @Resource
    private ServiceDoc docService;

    @GetMapping("Doc/vote/{id}")
    public CommonResponse vote(@PathVariable Long id) {
        CommonResponse commonResponse = new CommonResponse();
        docService.vote(id);
        return commonResponse;
    }
}
